/*
 * Copyright (C) 2012 by Hao Hou<ghost89413@gmail.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>*
 */
#include <stdio.h>
#include <X11/Xlib.h>
#include <log.h>
#include <init.h>
#include <userconf.h>
#include <getopt.h>
#include <stdlib.h>
#include <xim.h>
#include <loader.h>
#define DEFAULT_CONF "test.conf"
void PrintUsage()
{
	puts("-c\t\tLoad sepecified conf file");
	puts("-h\t\tPrint usage");
	puts("-i<imname>\tSepecify IM name");
	exit(0);
}
int main(int argc, char** argv)
{
	int ret;	
	atexit(Finalization);
	config = CreateConfig();
	if(NULL == config)
	{
		VINE_LOG_FATAL("Can not init config");
		return 1;
	}
	while(-1 != (ret = getopt(argc,argv,"c:hi:")))
	{
		switch(ret)
		{
			case 'c':
				SetValue(config,String,"UserConf", optarg);
				break;
			case 'i':
				SetValue(config,String,"IMName", optarg);
				break;
			case 'h':
			default:
				PrintUsage();
		}
	}

	char* conf_file;
	FetchValue(config,String,&conf_file,DEFAULT_CONF,"UserConf");
	InitConfig(conf_file);
	if(InitSignals() != 0)
	{
		VINE_LOG_FATAL("Signal Intialization failure, exiting...");
		return -1;
	}
	if(InitX() != 0)
	{
		VINE_LOG_FATAL("Xlib Initialization failure, exiting ...");
		return -1;
	}
	if(InitXIM(display) != 0)
	{
		VINE_LOG_FATAL("XIM Initialization failure, exiting ...");
		return -1;
	}
	if(InitIMEs() != 0)
	{
		VINE_LOG_FATAL("Input Method Intialization failture, exiting...");
		return -1;
	}
	if(InitUI() != 0)
	{
		VINE_LOG_FATAL("UI Intialization failture, exiting...");
		return -1;
	}
	while(1)
	{
		XEvent event;
		XNextEvent(display, &event);
		if(XFilterEvent(&event, 0)) continue;
	}
	return 0;
}
